<script setup>
import NavBarHeader from '@/components/NavBarHeader.vue'
import axiosInstance from '@/utils/request'
import { ref } from 'vue'
import { TOKEN } from '@/utils/constant'
import { useUserStore } from '@/stores/user'
import { showSuccessToast } from 'vant'
import router from '@/router'
const friendApplyList = ref([])
const store = useUserStore()
const isDisable = ref(false)
const getFriendApplyList = () => {
  axiosInstance
    .get('/friend/list', {
      headers: {
        TOKEN: store.token
      }
    })
    .then((response) => {
      friendApplyList.value = response.data.data
      console.log(friendApplyList.value)
    })
    .catch((error) => {
      console.log(error)
    })
}
const handleApply = (friendApply, event) => {
  axiosInstance
    .post(
      '/friend/response',
      {
        id: friendApply.id,
        status: event
      },
      {
        headers: {
          TOKEN: store.token
        }
      }
    )
    .then((response) => {
      showSuccessToast(response.data.msg)
      isDisable.value = true
      getFriendApplyList()
    })
    .catch((error) => {
      console.log(error)
    })
}
const handleContactDetail = (friendApply) => {
  router.push(`/contact/${friendApply.friendApplyId}`)
}
getFriendApplyList()
</script>

<template>
  <NavBarHeader>
    <template #title>好友申请</template>
  </NavBarHeader>
  <div class="contact-list">
    <ul>
      <li
        v-for="friendApply in friendApplyList"
        :key="friendApply.id"
        class="contact-item"
      >
        <img :src="friendApply.avatar" alt="头像" class="avatar" />
        <div class="contact-info" @click="handleContactDetail(friendApply)">
          <p style="color: #000" class="nickname">{{ friendApply.nickname }}</p>
        </div>
        <div
          v-if="
            friendApply.friendApplyId !== store.user.id &&
            friendApply.status === 3 || friendApply.status === 4
          "
        >
          <van-button
            :disabled="isDisable"
            @click="handleApply(friendApply, 1)"
            class="btn-item"
            size="small"
            plain
            type="danger"
            >拒绝</van-button
          >
          <van-button
            :disabled="isDisable"
            @click="handleApply(friendApply, 0)"
            class="btn-item"
            size="small"
            plain
            type="success"
            >同意</van-button
          >
        </div>
        <div v-else>
          <span style="color: #000" v-if="friendApply.status === 1"
            >已拒绝</span
          >
          <span style="color: #000" v-else-if="friendApply.status === 0"
            >已同意</span
          >
          <span style="color: #000" v-else-if="friendApply.status === 2"
            >等待验证</span
          >
        </div>
      </li>
    </ul>
  </div>
</template>

<style scoped>
.contact-list {
  padding: 10px;
}

.contact-item {
  display: flex;
  align-items: center;
  padding: 10px;
  margin-bottom: 10px; /* 每个联系人之间的间距 */
  border-radius: 8px; /* 圆角 */
  background-color: #fff; /* 背景颜色 */
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); /* 阴影效果 */
  transition: all 0.3s ease; /* 过渡效果 */
}
.contact-item:hover {
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2); /* 悬停时的阴影效果 */
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 10px;
}

.contact-info {
  flex-grow: 1; /* 使信息部分占据可用空间 */
}

.nickname {
  font-size: 16px;
  font-weight: bold;
}
.btn-item {
  margin: 5px;
  color: #000;
}
</style>
